package com.htj.admin.controller;

import com.htj.admin.domain.Cart;
import com.htj.admin.service.CartServiceInf;
import com.htj.common.Result;
import com.htj.util.JWTUtil;
import com.htj.vo.PageListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "cart")
@CrossOrigin
public class CartController {
    @Autowired
    CartServiceInf cardServiceInf;
    /**
     * 购物车新增菜品
     * @param
     * @return
     */
    @PostMapping("/save")
    public Result<String> add(@RequestBody Cart cart,@RequestHeader String token){
        token = token.substring(1).substring(0,token.length() - 2);
        String id = JWTUtil.verify(token).getClaim("id").asString();
        Cart c = cardServiceInf.getByFoodIdAndUserId(Integer.valueOf(cart.getFoodId()),Integer.valueOf(id));
        boolean bool = false;
        if(c != null){
            Map<String,String> map = new HashMap<>();
            int s1 = Integer.valueOf(c.getNum());
            int s2 = Integer.valueOf(cart.getNum());
            String res = String.valueOf(s1 + s2);
            map.put("num",res);
            map.put("foodId",c.getFoodId());
            map.put("userId",id);
            bool = cardServiceInf.handleChange(map);
            return bool?Result.succ():Result.fail();
        }
        bool = cardServiceInf.add(cart);
        return bool?Result.succ():Result.fail();
    }

    /**
     * 传入foodId
     * @param
     * @return
     */
    @GetMapping("/getByFoodIdAndUserId")
    public Result<Cart> getByFoodIdAndUserId(@RequestParam Integer foodId,@RequestParam Integer userId){
        Cart cart = cardServiceInf.getByFoodIdAndUserId(foodId,userId);
        return Result.succ(cart);
    }
    /**
     * 修改购物车菜品
     * @param
     * @return
     */
    @PutMapping("/save")
    public Result<String> update(@RequestBody Cart card){
        boolean bool = cardServiceInf.update(card);
        return bool?Result.succ():Result.fail();
    }
    /**
     * 分页数据
     * @param currentPage
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/pageList")
    public Result<PageListVo> pageList(@RequestParam String currentPage,
                                       @RequestParam String pageSize,
                                       @RequestParam String name,
                                       @RequestHeader String token){
        HashMap<String, String> map = new HashMap<>();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        token = token.substring(1).substring(0,token.length() - 2);
        String id = JWTUtil.verify(token).getClaim("id").asString();
        map.put("id",id);
        map.put("name",name);
        PageListVo<Cart> pageList = cardServiceInf.pageList(map);
        return Result.succ(pageList);
    }

    /**
     * 删除购物车菜品
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Result<String> delete(@PathVariable Integer id){
        boolean bool = cardServiceInf.delete(id);
        return bool?Result.succ():Result.fail();
    }
    /**
     * 批量删除购物车菜品
     * @param list
     * @return
     */
    @PostMapping("/deleteByIds")
    public Result<String> deleteByIds(@RequestBody List<Integer> list){
        boolean bool = true;
        for(Integer id : list){
            bool = cardServiceInf.delete(id);
        }
        return bool?Result.succ():Result.fail();
    }
    @PostMapping("/handleChange")
    public Result<String> handleChange(@RequestBody Map<String,String> map,@RequestHeader String token){
        token = token.substring(1).substring(0,token.length() - 2);
        String id = JWTUtil.verify(token).getClaim("id").asString();
        map.put("userId",id);
        boolean bool = cardServiceInf.handleChange(map);
        return bool?Result.succ():Result.fail();
    }
}
